{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.13 \u521b\u5efa\u6570\u636e\u5904\u7406\u7ba1\u9053\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u4ee5\u6570\u636e\u7ba1\u9053(\u7c7b\u4f3cUnix\u7ba1\u9053)\u7684\u65b9\u5f0f\u8fed\u4ee3\u5904\u7406\u6570\u636e\u3002\n\u6bd4\u5982\uff0c\u4f60\u6709\u4e2a\u5927\u91cf\u7684\u6570\u636e\u9700\u8981\u5904\u7406\uff0c\u4f46\u662f\u4e0d\u80fd\u5c06\u5b83\u4eec\u4e00\u6b21\u6027\u653e\u5165\u5185\u5b58\u4e2d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u751f\u6210\u5668\u51fd\u6570\u662f\u4e00\u4e2a\u5b9e\u73b0\u7ba1\u9053\u673a\u5236\u7684\u597d\u529e\u6cd5\u3002\n\u4e3a\u4e86\u6f14\u793a\uff0c\u5047\u5b9a\u4f60\u8981\u5904\u7406\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u65e5\u5fd7\u6587\u4ef6\u76ee\u5f55\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "foo/\n    access-log-012007.gz\n    access-log-022007.gz\n    access-log-032007.gz\n    ...\n    access-log-012008\nbar/\n    access-log-092007.bz2\n    ...\n    access-log-022008"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5047\u8bbe\u6bcf\u4e2a\u65e5\u5fd7\u6587\u4ef6\u5305\u542b\u8fd9\u6837\u7684\u6570\u636e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "124.115.6.12 - - [10/Jul/2012:00:18:50 -0500] \"GET /robots.txt ...\" 200 71\n210.212.209.67 - - [10/Jul/2012:00:18:51 -0500] \"GET /ply/ ...\" 200 11875\n210.212.209.67 - - [10/Jul/2012:00:18:51 -0500] \"GET /favicon.ico ...\" 404 369\n61.135.216.105 - - [10/Jul/2012:00:20:04 -0500] \"GET /blog/atom.xml ...\" 304 -\n..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7531\u591a\u4e2a\u6267\u884c\u7279\u5b9a\u4efb\u52a1\u72ec\u7acb\u4efb\u52a1\u7684\u7b80\u5355\u751f\u6210\u5668\u51fd\u6570\u7ec4\u6210\u7684\u5bb9\u5668\u3002\u5c31\u50cf\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\nimport fnmatch\nimport gzip\nimport bz2\nimport re\n\ndef gen_find(filepat, top):\n    '''\n    Find all filenames in a directory tree that match a shell wildcard pattern\n    '''\n    for path, dirlist, filelist in os.walk(top):\n        for name in fnmatch.filter(filelist, filepat):\n            yield os.path.join(path,name)\n\ndef gen_opener(filenames):\n    '''\n    Open a sequence of filenames one at a time producing a file object.\n    The file is closed immediately when proceeding to the next iteration.\n    '''\n    for filename in filenames:\n        if filename.endswith('.gz'):\n            f = gzip.open(filename, 'rt')\n        elif filename.endswith('.bz2'):\n            f = bz2.open(filename, 'rt')\n        else:\n            f = open(filename, 'rt')\n        yield f\n        f.close()\n\ndef gen_concatenate(iterators):\n    '''\n    Chain a sequence of iterators together into a single sequence.\n    '''\n    for it in iterators:\n        yield from it\n\ndef gen_grep(pattern, lines):\n    '''\n    Look for a regex pattern in a sequence of lines\n    '''\n    pat = re.compile(pattern)\n    for line in lines:\n        if pat.search(line):\n            yield line"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5c06\u8fd9\u4e9b\u51fd\u6570\u8fde\u8d77\u6765\u521b\u5efa\u4e00\u4e2a\u5904\u7406\u7ba1\u9053\u3002\n\u6bd4\u5982\uff0c\u4e3a\u4e86\u67e5\u627e\u5305\u542b\u5355\u8bcdpython\u7684\u6240\u6709\u65e5\u5fd7\u884c\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "lognames = gen_find('access-log*', 'www')\nfiles = gen_opener(lognames)\nlines = gen_concatenate(files)\npylines = gen_grep('(?i)python', lines)\nfor line in pylines:\n    print(line)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u5c06\u6765\u7684\u65f6\u5019\u4f60\u60f3\u6269\u5c55\u7ba1\u9053\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u5728\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e2d\u5305\u88c5\u6570\u636e\u3002\n\u6bd4\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a\u7248\u672c\u8ba1\u7b97\u51fa\u4f20\u8f93\u7684\u5b57\u8282\u6570\u5e76\u8ba1\u7b97\u5176\u603b\u548c\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "lognames = gen_find('access-log*', 'www')\nfiles = gen_opener(lognames)\nlines = gen_concatenate(files)\npylines = gen_grep('(?i)python', lines)\nbytecolumn = (line.rsplit(None,1)[1] for line in pylines)\nbytes = (int(x) for x in bytecolumn if x != '-')\nprint('Total', sum(bytes))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ee5\u7ba1\u9053\u65b9\u5f0f\u5904\u7406\u6570\u636e\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u5404\u7c7b\u5176\u4ed6\u95ee\u9898\uff0c\u5305\u62ec\u89e3\u6790\uff0c\u8bfb\u53d6\u5b9e\u65f6\u6570\u636e\uff0c\u5b9a\u65f6\u8f6e\u8be2\u7b49\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u7406\u89e3\u4e0a\u8ff0\u4ee3\u7801\uff0c\u91cd\u70b9\u662f\u8981\u660e\u767d yield \u8bed\u53e5\u4f5c\u4e3a\u6570\u636e\u7684\u751f\u4ea7\u8005\u800c for \u5faa\u73af\u8bed\u53e5\u4f5c\u4e3a\u6570\u636e\u7684\u6d88\u8d39\u8005\u3002\n\u5f53\u8fd9\u4e9b\u751f\u6210\u5668\u88ab\u8fde\u5728\u4e00\u8d77\u540e\uff0c\u6bcf\u4e2a yield \u4f1a\u5c06\u4e00\u4e2a\u5355\u72ec\u7684\u6570\u636e\u5143\u7d20\u4f20\u9012\u7ed9\u8fed\u4ee3\u5904\u7406\u7ba1\u9053\u7684\u4e0b\u4e00\u9636\u6bb5\u3002\n\u5728\u4f8b\u5b50\u6700\u540e\u90e8\u5206\uff0c sum() \u51fd\u6570\u662f\u6700\u7ec8\u7684\u7a0b\u5e8f\u9a71\u52a8\u8005\uff0c\u6bcf\u6b21\u4ece\u751f\u6210\u5668\u7ba1\u9053\u4e2d\u63d0\u53d6\u51fa\u4e00\u4e2a\u5143\u7d20\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u65b9\u5f0f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u7279\u70b9\u662f\u6bcf\u4e2a\u751f\u6210\u5668\u51fd\u6570\u5f88\u5c0f\u5e76\u4e14\u90fd\u662f\u72ec\u7acb\u7684\u3002\u8fd9\u6837\u7684\u8bdd\u5c31\u5f88\u5bb9\u6613\u7f16\u5199\u548c\u7ef4\u62a4\u5b83\u4eec\u4e86\u3002\n\u5f88\u591a\u65f6\u5019\uff0c\u8fd9\u4e9b\u51fd\u6570\u5982\u679c\u6bd4\u8f83\u901a\u7528\u7684\u8bdd\u53ef\u4ee5\u5728\u5176\u4ed6\u573a\u666f\u91cd\u590d\u4f7f\u7528\u3002\n\u5e76\u4e14\u6700\u7ec8\u5c06\u8fd9\u4e9b\u7ec4\u4ef6\u7ec4\u5408\u8d77\u6765\u7684\u4ee3\u7801\u770b\u4e0a\u53bb\u975e\u5e38\u7b80\u5355\uff0c\u4e5f\u5f88\u5bb9\u6613\u7406\u89e3\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u7684\u5185\u5b58\u6548\u7387\u4e5f\u4e0d\u5f97\u4e0d\u63d0\u3002\u4e0a\u8ff0\u4ee3\u7801\u5373\u4fbf\u662f\u5728\u4e00\u4e2a\u8d85\u5927\u578b\u6587\u4ef6\u76ee\u5f55\u4e2d\u4e5f\u80fd\u5de5\u4f5c\u7684\u5f88\u597d\u3002\n\u4e8b\u5b9e\u4e0a\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86\u8fed\u4ee3\u65b9\u5f0f\u5904\u7406\uff0c\u4ee3\u7801\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u53ea\u9700\u8981\u5f88\u5c0f\u5f88\u5c0f\u7684\u5185\u5b58\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8c03\u7528 gen_concatenate() \u51fd\u6570\u7684\u65f6\u5019\u4f60\u53ef\u80fd\u4f1a\u6709\u4e9b\u4e0d\u592a\u660e\u767d\u3002\n\u8fd9\u4e2a\u51fd\u6570\u7684\u76ee\u7684\u662f\u5c06\u8f93\u5165\u5e8f\u5217\u62fc\u63a5\u6210\u4e00\u4e2a\u5f88\u957f\u7684\u884c\u5e8f\u5217\u3002\nitertools.chain() \u51fd\u6570\u540c\u6837\u6709\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u662f\u5b83\u9700\u8981\u5c06\u6240\u6709\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\n\u5728\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u5199\u7c7b\u4f3c\u8fd9\u6837\u7684\u8bed\u53e5 lines = itertools.chain(*files) \uff0c\n\u8fd9\u5c06\u5bfc\u81f4 gen_opener() \u751f\u6210\u5668\u88ab\u63d0\u524d\u5168\u90e8\u6d88\u8d39\u6389\u3002\n\u4f46\u7531\u4e8e gen_opener() \u751f\u6210\u5668\u6bcf\u6b21\u751f\u6210\u4e00\u4e2a\u6253\u5f00\u8fc7\u7684\u6587\u4ef6\uff0c\n\u7b49\u5230\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u6b65\u9aa4\u65f6\u6587\u4ef6\u5c31\u5173\u95ed\u4e86\uff0c\u56e0\u6b64 chain() \u5728\u8fd9\u91cc\u4e0d\u80fd\u8fd9\u6837\u4f7f\u7528\u3002\n\u4e0a\u9762\u7684\u65b9\u6848\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "gen_concatenate() \u51fd\u6570\u4e2d\u51fa\u73b0\u8fc7 yield from \u8bed\u53e5\uff0c\u5b83\u5c06 yield \u64cd\u4f5c\u4ee3\u7406\u5230\u7236\u751f\u6210\u5668\u4e0a\u53bb\u3002\n\u8bed\u53e5 yield from it \u7b80\u5355\u7684\u8fd4\u56de\u751f\u6210\u5668 it \u6240\u4ea7\u751f\u7684\u6240\u6709\u503c\u3002\n\u5173\u4e8e\u8fd9\u4e2a\u6211\u4eec\u57284.14\u5c0f\u8282\u4f1a\u6709\u66f4\u8fdb\u4e00\u6b65\u7684\u63cf\u8ff0\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7ba1\u9053\u65b9\u5f0f\u5e76\u4e0d\u662f\u4e07\u80fd\u7684\u3002\n\u6709\u65f6\u5019\u4f60\u60f3\u7acb\u5373\u5904\u7406\u6240\u6709\u6570\u636e\u3002\n\u7136\u800c\uff0c\u5373\u4fbf\u662f\u8fd9\u79cd\u60c5\u51b5\uff0c\u4f7f\u7528\u751f\u6210\u5668\u7ba1\u9053\u4e5f\u53ef\u4ee5\u5c06\u8fd9\u7c7b\u95ee\u9898\u4ece\u903b\u8f91\u4e0a\u53d8\u4e3a\u5de5\u4f5c\u6d41\u7684\u5904\u7406\u65b9\u5f0f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "David Beazley \u5728\u4ed6\u7684\nGenerator Tricks for Systems Programmers\n\u6559\u7a0b\u4e2d\u5bf9\u4e8e\u8fd9\u79cd\u6280\u672f\u6709\u975e\u5e38\u6df1\u5165\u7684\u8bb2\u89e3\u3002\u53ef\u4ee5\u53c2\u8003\u8fd9\u4e2a\u6559\u7a0b\u83b7\u53d6\u66f4\u591a\u7684\u4fe1\u606f\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}